<template>
    <el-dialog :title="title" :visible.sync="cateMdataOpen" width="60%" :before-close="closeDelDialog">
        <div>
            <el-form :model="queryAddParams" ref="queryAddForm"  :inline="true" v-show="showSearch"
                label-width="68px">
                <el-form-item label="显示名称" prop="fieldCaption">
                    <el-input v-model="queryAddParams.fieldCaption" placeholder="请输入显示名称" clearable style="width: 240px"
                        @keyup.enter.native="addHandleQuery" />
                </el-form-item>
                <el-form-item label="字段名称" prop="fieldName">
                    <el-input v-model="queryAddParams.fieldName" placeholder="请输入字段名称" clearable style="width: 240px" @input="toUpperCase"
                        @keyup.enter.native="addHandleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search"  @click="addHandleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh"  @click="addResetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="addList" height="480" border @selection-change="addSelectionChange">
                <el-table-column type="selection" width="50" align="center" />
                <el-table-column label="序号" align="center" type="index" />
                <el-table-column label="显示名称" width="150" align="center" key="fieldCaption" prop="fieldCaption">

                </el-table-column>
                <el-table-column label="字段名称" align="center" key="fieldName" prop="fieldName"
                    :show-overflow-tooltip="true" />
                <el-table-column label="字段类型" align="center" key="fieldType" prop="fieldType" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <span v-for="dict in dict.type.basic_data_fieldtype" :key="dict.value">{{ scope.row.fieldType ==
                            dict.value ? dict.label : '' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="长度" align="center" key="fieldLength" prop="fieldLength"
                    :show-overflow-tooltip="true" />
                <el-table-column label="是否公共字段" align="center" key="isCommon" prop="isCommon" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        {{ scope.row.isCommon == 1 ? "是" : "否" }}
                    </template>
                </el-table-column>

            </el-table>

            <pagination v-show="addTotal > 0" :total="addTotal" :page.sync="queryAddParams.pageNum"
                :limit.sync="queryAddParams.pageSize" @pagination="getCateMdatalistGroup" />
        </div>

        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDelDialog">取 消</el-button>
            <el-button type="primary" @click="addSubmitTap">保 存</el-button>
        </span>
    </el-dialog>
</template>
<script>
import { addCateMdataList, cateMdatalistGroupid } from "@/api/archive/archivesCategory.js";
import { addMdataList, addMdataDataid } from "@/api/manage/metaData";
import { addMdataList1, addMdataDataid1 } from "@/api/archive/metaData.js";
export default {
    dicts: ['sys_normal_disable', 'basic_data_fieldtype'],
    // cateIsType  0=应用定义档案门类里面的元数据方案明细列表添加    1=应用定义里面的元数据方案明细列表添加  2=manager下面的模板管理的元数据方案添加
    watch: {
        addParams: {
            deep: true,
            immediate: true, // 监听到后，立即执行 handler方法
            handler: function (newVal, oldVal) {

                this.queryAddParams.groupId = newVal.queryAddParams.groupId;
                this.queryAddParams.type = newVal.queryAddParams.type;
                this.cateIsType = newVal.cateIsType;
                // if (newVal.cateIsType == "0") {
                this.getCateMdatalistGroup();
                // }else if(newVal.cateIsType=="2"){
                //     this.getaddMdataList();
                // }else{
                //     console.log(newVal, "_______________________dklsfjl")
                //     this.getaddMdataList0();
                // }
            },

        }
    },
    props: ['cateMdataOpen', 'addParams'],
    data() {
        return {
            title: "添加基础数据",
            addList: [],
            // 遮罩层
            loading: true,
            cateIsType: "",
            //添加到数据明细基础列表
            queryAddParams: {
                pageNum: 1,
                pageSize: 10,
                groupId: undefined,
                fieldCaption: "",
                fieldName: "",
                type: undefined,
            },
            // 显示搜索条件
            showSearch: true,
            //添加的列表数据  总条数
            addTotal: 0,
            //添加列表的选中数组
            Ids: [],
        }
    },

    methods: {
        // 初始化方法
        init(val) {
            console.log(val)
            //   this.activityId = val   //接收父组件传递的值
            //   this.dialogFormVisible = true;
            //   this.getActivityInfo()
        },

        //应用定义  档案门类下面的元数据明细添加基础数据列表
        getCateMdatalistGroup() {
            if (this.cateIsType == "0" || this.cateIsType == "1") {
                cateMdatalistGroupid(this.queryAddParams).then(response => {
                    this.addList = response.rows;
                    this.addTotal = response.total;
                    this.loading = false;
                });
            } else {
                addMdataDataid(this.queryAddParams).then(response => {
                    this.addList = response.rows;
                    this.addTotal = response.total;
                    this.loading = false;
                });
            }
        },
        //立档单位下面的元数据明细添加基础数据列表
        getaddMdataList() {
            addMdataDataid(this.queryAddParams).then(response => {
                this.addList = response.rows;
                this.addTotal = response.total;
                this.loading = false;
            }
            );
        },

        getaddMdataList0() {
            console.log("sdklfjsdkljfsdkljfsdklfjsdkljf")
            addMdataDataid1(this.queryAddParams).then(response => {
                console.log(response.rows)
                this.addList = response.rows;
                this.addTotal = response.total;
                this.loading = false;
            }
            );
        },

        // 添加多选框选中数据
        addSelectionChange(selection) {
            this.Ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        //重置
        addResetQuery() {
            this.$refs.queryAddForm.resetFields();
            this.getCateMdatalistGroup();
        },
        toUpperCase() {
            this.queryAddParams.fieldName = this.queryAddParams.fieldName.toUpperCase()
        },

        //搜索
        addHandleQuery() {
            this.getCateMdatalistGroup();
        },

        //元数据明细添加  提交操作
        addSubmitTap() {
            console.log(this.Ids, this.cateIsType, this.addParams, "this.queryAddParams.cateIsType")
            if (this.Ids.length !== 0) {

                if (this.cateIsType == "0") {
                    addCateMdataList({
                        categoryId: this.addParams.queryAddParams.categoryId,
                        ids: this.Ids.toString(),
                        type: this.addParams.queryAddParams.type
                    }).then(response => {
                        this.$modal.msgSuccess("添加成功");
                        this.closeDelDialog();
                        this.$emit("addSubmitTap")
                    });
                } else if (this.cateIsType == "2") {
                    console.log(this.Ids.toString(), "sdlkjflksdjf")
                    addMdataList({
                        groupId: this.addParams.queryAddParams.groupId,
                        ids: this.Ids.toString()
                    }).then(response => {
                        this.$modal.msgSuccess("添加成功");
                        this.closeDelDialog();
                        this.$emit("addSubmitTap")
                    });
                } else {
                    addMdataList1({
                        groupId: this.addParams.queryAddParams.groupId,
                        ids: this.Ids.toString()
                    }).then(response => {
                        this.$modal.msgSuccess("添加成功");
                        this.closeDelDialog();
                        this.$emit("addSubmitTap")
                    });
                }

            } else {
                this.$modal.msgError("请选择要添加的数据");
            }
        },
        closeDelDialog() {
            this.$emit("closeMdataDelDialog")
        }

    }
}

</script>
<style lang="scss" scoped>
::v-deep .el-dialog {
    display: flex;
    flex-direction: column;
    margin: 0 !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-height: calc(100% - 30px);
    max-width: calc(100% - 30px);
}

::v-deep .el-dialog .el-dialog__body {
    flex: 1;
    overflow: auto;
}
</style>

